﻿using System;
using System.Collections.Generic;
using System.IO;
using Unity.Collections;
using UnityEditor;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.CityEditor
{
    public class RoomEventEditorManager : MonoBehaviour
    {
        public class RoomGoodsEditorData
        {
            public string GoodsName;
            public int UnlockBuildId;
            public int UnlockLevel;
            public int FacilityId;
            public List<GoodsPointEditorData> pointDataList;
        }

        public struct DoubleVector3
        {
            public double x;
            public double y;
            public double z;

            public DoubleVector3(Vector3 vec)
            {
                x = Math.Round(vec.x, 2);
                y = Math.Round(vec.y, 2);
                z = Math.Round(vec.z, 2);
            }
        }
    
        public class GoodsPointEditorData
        {
            public int Index;
            public int eventId;
            public int EventGroup;
            public DoubleVector3 Position;
            public DoubleVector3 Rotate;
            public string GoodsTriggerName;
            public string MoveGoodsName;
            public List<PointData> MoveGoodsPosList;
        }

        public class PointData
        {
            public DoubleVector3 Position;
            public DoubleVector3 Rotate;
        }

        public class CityRoomGoodsEditorData
        {
            public int roomId;
            public List<DoubleVector3> birthPointPosList;
            public List<DoubleVector3> specialPointPosList;
            public List<RoomGoodsEditorData> goodsList;
            public List<DoubleVector3> MovePointList;
            public List<PointData> WaitPointList;
            public List<PointData> FrontStaffWaitePointList;
            public List<PointData> BackStaffWaitePointList;
            public List<List<PointData>> MultiWaitPointVecList; // 多个排队队列的点位
            public List<PointData> BusinessNpcJumpToRoomPointList; // timeline跳过时，npc直接生成在房间附近指定点位置配置。
            public List<List<PointData>> OccupyAreaBirthPointVecList; // 占领区域的雇员顾客生成点
        }
        [Serializable]
        public class CityRoomListEditorData
        {
            public List<CityRoomGoodsEditorData> roomGoodsList;
        }

        
        public void Save(string editorConfigPath)
        {
#if UNITY_EDITOR
            Debug.Log("开始导出！");
            // 遍历子节点中的RoomEventData进行数据导出
            var roomEventList = transform.GetComponentsInChildren<RoomEventData>();
            
            // 保存编辑器配置
            CityRoomListEditorData roomListConfig = new CityRoomListEditorData();
            roomListConfig.roomGoodsList = new List<CityRoomGoodsEditorData>();
            if (roomEventList != null)
            {
                for (int i = 0; i < roomEventList.Length; i++)
                {
                    CityRoomGoodsEditorData roomGoodsConfig = new CityRoomGoodsEditorData();
                    var roomEventData = roomEventList[i];
                    roomGoodsConfig.roomId = roomEventData.RoomId;
                    
                    roomGoodsConfig.birthPointPosList = new List<DoubleVector3>();
                    var birthPointRoot = roomEventData.transform.Find("BirthPointRoot");
                    if (birthPointRoot != null)
                    {
                        for (int j = 0; j < birthPointRoot.childCount; j++)
                        {
                            roomGoodsConfig.birthPointPosList.Add(new DoubleVector3(birthPointRoot.GetChild(j).localPosition));
                        }
                    }

                    roomGoodsConfig.specialPointPosList = new List<DoubleVector3>();
                    var specialPointRoot = roomEventData.transform.Find("SpecialPointRoot");
                    if (specialPointRoot != null)
                    {
                        var inPoint = specialPointRoot.Find("InRoomPoint");
                        if (inPoint != null)
                        {
                            roomGoodsConfig.specialPointPosList.Add(new DoubleVector3(inPoint.localPosition));
                        }

                        var outPoint = specialPointRoot.Find("OutRoomPoint");
                        if (outPoint != null)
                        {
                            roomGoodsConfig.specialPointPosList.Add(new DoubleVector3(outPoint.localPosition));
                        }

                        var hidePoint = specialPointRoot.Find("HidePoint");
                        if (hidePoint != null)
                        {
                            roomGoodsConfig.specialPointPosList.Add(new DoubleVector3(hidePoint.localPosition));
                        }

                        var inPoint2 = specialPointRoot.Find("InRoomPoint_02");
                        if (inPoint2 != null)
                        {
                            roomGoodsConfig.specialPointPosList.Add(new DoubleVector3(inPoint2.localPosition));
                        }
                        
                        var outPoint2 = specialPointRoot.Find("OutRoomPoint_02");
                        if (outPoint2 != null)
                        {
                            roomGoodsConfig.specialPointPosList.Add(new DoubleVector3(outPoint2.localPosition));
                        }
                        
                        var frontBirthPoint = specialPointRoot.Find("FrontBirthPoint");
                        if (frontBirthPoint != null)
                        {
                            roomGoodsConfig.specialPointPosList.Add(new DoubleVector3(frontBirthPoint.localPosition));
                        }
                        
                        var backBirthPoint = specialPointRoot.Find("BackBirthPoint");
                        if (backBirthPoint != null)
                        {
                            roomGoodsConfig.specialPointPosList.Add(new DoubleVector3(backBirthPoint.localPosition));
                        }

                        for (int j = 1; j <= CityRealAiCustomerBus.k_BusMaxCount; ++j)
                        {
                            var downPoint = specialPointRoot.Find(string.Format("Bus_Car_Point_Downcar_0{0}", j));
                            if (downPoint != null)
                            {
                                roomGoodsConfig.specialPointPosList.Add(new DoubleVector3(downPoint.localPosition));
                            }
                        }
                    }
                    
                    roomGoodsConfig.goodsList = new List<RoomGoodsEditorData>();
                    roomGoodsConfig.MovePointList = new List<DoubleVector3>();
                    
                    for (int j = 0; j < roomEventData.RoomGoodsList.Count; j++)
                    {
                        var goodsData = roomEventData.RoomGoodsList[j];
                        RoomGoodsEditorData roomGoodsEditorData = new RoomGoodsEditorData();
                        roomGoodsEditorData.UnlockBuildId = goodsData.UnlockBuildId;
                        roomGoodsEditorData.UnlockLevel = goodsData.UnlockLevel;
                        roomGoodsEditorData.FacilityId = goodsData.FacilityId;
                        roomGoodsEditorData.pointDataList = new List<GoodsPointEditorData>();
                        for (int k = 0; k < goodsData.PointList.Count; k++)
                        {
                            var goodsPointData = goodsData.PointList[k];
                            if (goodsPointData.eventId <= 0) // 废弃事件
                            {
                                continue;
                            }
                            GoodsPointEditorData eventPointEditorData = new GoodsPointEditorData();
                            eventPointEditorData.Index = j;
                            eventPointEditorData.eventId = goodsPointData.eventId;
                            eventPointEditorData.EventGroup = (int) goodsPointData.Group;
                            if (goodsPointData.point)
                            {
                                eventPointEditorData.Position = new DoubleVector3(goodsPointData.point.localPosition);
                                eventPointEditorData.Rotate = new DoubleVector3(goodsPointData.point.localEulerAngles);
                            }
                            else
                            {
                                eventPointEditorData.Position = new DoubleVector3(Vector3.zero);
                                eventPointEditorData.Rotate = new DoubleVector3(Vector3.zero);
                            }
                            eventPointEditorData.GoodsTriggerName = goodsPointData.GoodsTriggerName;
                            //交互物触发的位置坐标和旋转
                            eventPointEditorData.MoveGoodsName = string.Empty;
                            eventPointEditorData.MoveGoodsPosList = new List<PointData>();
                            if (goodsPointData.MovedTriggerObj && goodsPointData.MovedTriggerObj.childCount > 0)
                            {
                                eventPointEditorData.MoveGoodsName = goodsPointData.MovedTriggerObj.name;
                                for (int l = 0; l < goodsPointData.MovedTriggerObj.childCount; l++)
                                {
                                    var moveGoodsTran = goodsPointData.MovedTriggerObj.GetChild(l);
                                    var pointData = new PointData();
                                    pointData.Position = new DoubleVector3(moveGoodsTran.localPosition);
                                    pointData.Rotate = new DoubleVector3(moveGoodsTran.localEulerAngles);
                                    eventPointEditorData.MoveGoodsPosList.Add(pointData);
                                }
                            }
                            
                            roomGoodsEditorData.pointDataList.Add(eventPointEditorData);
                        }
                        if (goodsData.Goods != null)
                        {
                            roomGoodsEditorData.GoodsName = goodsData.Goods.name;
                        }
                        else
                        {
                            roomGoodsEditorData.GoodsName = string.Empty;
                        }
                        roomGoodsConfig.goodsList.Add(roomGoodsEditorData);
                    }
                    roomListConfig.roomGoodsList.Add(roomGoodsConfig);
                    
                    var movePointRoot = roomEventData.transform.Find("MovePointRoot");
                    if (movePointRoot != null)
                    {
                        for (int j = 0; j < movePointRoot.childCount; j++)
                        {
                            roomGoodsConfig.MovePointList.Add(new DoubleVector3(movePointRoot.GetChild(j).localPosition));
                        }
                    }

                    roomGoodsConfig.WaitPointList = new List<PointData>();
                    var waitPointRoot = roomEventData.transform.Find("WaitePointRoot");
                    if (waitPointRoot != null)
                    {
                        for (int j = 0; j < waitPointRoot.childCount; j++)
                        {
                            var child = waitPointRoot.GetChild(j);
                            var waitPoint = new PointData();
                            waitPoint.Position = new DoubleVector3(child.localPosition);
                            waitPoint.Rotate = new DoubleVector3(child.localEulerAngles);
                            roomGoodsConfig.WaitPointList.Add(waitPoint);
                        }
                    }
                    
                    roomGoodsConfig.FrontStaffWaitePointList = new List<PointData>();
                    var frontStaffWaitPointRoot = roomEventData.transform.Find("FrontStaffWaitPointRoot");
                    if (frontStaffWaitPointRoot != null)
                    {
                        for (int j = 0; j < frontStaffWaitPointRoot.childCount; j++)
                        {
                            var child = frontStaffWaitPointRoot.GetChild(j);
                            var waitPoint = new PointData();
                            waitPoint.Position = new DoubleVector3(child.localPosition);
                            waitPoint.Rotate = new DoubleVector3(child.localEulerAngles);
                            roomGoodsConfig.FrontStaffWaitePointList.Add(waitPoint);
                        }
                    }
                    
                    roomGoodsConfig.BackStaffWaitePointList = new List<PointData>();
                    var backStaffWaitPointRoot = roomEventData.transform.Find("BackStaffWaitPointRoot");
                    if (backStaffWaitPointRoot != null)
                    {
                        for (int j = 0; j < backStaffWaitPointRoot.childCount; j++)
                        {
                            var child = backStaffWaitPointRoot.GetChild(j);
                            var waitPoint = new PointData();
                            waitPoint.Position = new DoubleVector3(child.localPosition);
                            waitPoint.Rotate = new DoubleVector3(child.localEulerAngles);
                            roomGoodsConfig.BackStaffWaitePointList.Add(waitPoint);
                        }
                    }

                    roomGoodsConfig.MultiWaitPointVecList = new List<List<PointData>>();
                    var multiWaitPointVecRoot = roomEventData.transform.Find("MultiWaitePointVecRoot");
                    if (multiWaitPointVecRoot)
                    {
                        foreach (Transform waitPointVec in multiWaitPointVecRoot)
                        {
                            List<PointData> points = new List<PointData>();
                            roomGoodsConfig.MultiWaitPointVecList.Add(points);
                            foreach (Transform child in waitPointVec)
                            {
                                var waitPoint = new PointData();
                                waitPoint.Position = new DoubleVector3(child.localPosition);
                                waitPoint.Rotate = new DoubleVector3(child.localEulerAngles);
                                points.Add(waitPoint);
                            }
                        }
                    }
                    
                    roomGoodsConfig.OccupyAreaBirthPointVecList = new List<List<PointData>>();
                    var occupyAreaBirthPointRoot = roomEventData.transform.Find("OccupyAreaBirthPointRoot");
                    if (occupyAreaBirthPointRoot)
                    {
                        foreach (Transform waitPointVec in occupyAreaBirthPointRoot)
                        {
                            List<PointData> points = new List<PointData>();
                            roomGoodsConfig.OccupyAreaBirthPointVecList.Add(points);
                            foreach (Transform child in waitPointVec)
                            {
                                var waitPoint = new PointData();
                                waitPoint.Position = new DoubleVector3(child.localPosition);
                                waitPoint.Rotate = new DoubleVector3(child.localEulerAngles);
                                points.Add(waitPoint);
                            }
                        }
                    }
                        
                    roomGoodsConfig.BusinessNpcJumpToRoomPointList = new List<PointData>();
                    var businessPointRoot = roomEventData.transform.Find("BusspathPointRoot");
                    if (businessPointRoot)
                    {
                        for (int j = 0; j < businessPointRoot.childCount; j++)
                        {
                            var child = businessPointRoot.GetChild(j);
                            var businessPoint = new PointData();
                            businessPoint.Position = new DoubleVector3(child.localPosition);
                            businessPoint.Rotate = new DoubleVector3(child.localEulerAngles);
                            roomGoodsConfig.BusinessNpcJumpToRoomPointList.Add(businessPoint);
                        }
                    }
                }
                var jsonText = LitJson.JsonMapper.ToJson(roomListConfig);
                var luaText = JsonToLua.ConvertLua(jsonText);
                FileStream fs = new FileStream(editorConfigPath, FileMode.Create, FileAccess.Write);
                using (StreamWriter writer = new StreamWriter(fs))
                {
                    writer.Write(luaText);
                    writer.Flush();
                    writer.Close();
                    fs.Close();
                    Debug.Log("导出结束！");
                    EditorUtility.DisplayDialog("导出成功", "导出成功！", "确定");
                }
                AssetDatabase.Refresh();
            }
            else
            {
                Debug.LogError("没有房间交互物数据！！！");
            }
            
#endif
        }
        
    }
}
